package com.androidgame.fx.impl;

import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;

import com.androidgame.fx.Graphics;

public class AndroidGraphics implements Graphics {
	
    AssetManager 	assets;
    Resources 		resources;
    Bitmap 			frameBuffer;
    Canvas 			canvas;
    Paint 			paint;
    Rect 			srcRect;
    Rect 			dstRect;

    public AndroidGraphics(AssetManager assets, Resources resources, Bitmap frameBuffer) {
        this.assets = assets;
        this.resources = resources;
        this.frameBuffer = frameBuffer;
        this.canvas = new Canvas(frameBuffer);
        this.paint = new Paint();
        paint.setAntiAlias(true);
        srcRect = new Rect();
        dstRect = new Rect();
    }

    @Override
    public Bitmap newBitmap(String fileName) {
        InputStream in = null;
        Bitmap bitmap = null;
        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return bitmap;
    }
    
    @Override
    public Bitmap newBitmap(int resourceId) {
        Bitmap bitmap = null;
        bitmap = BitmapFactory.decodeResource(resources, resourceId);
        
        return bitmap;
    }
    
    @Override
    public Bitmap newBitmap(String fileName, int width, int height) {
        InputStream in = null;
        Bitmap bitmap = null;
        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in);
           	bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        } catch (Exception e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return bitmap;
    }
    
    @Override
    public Bitmap newBitmap(int resourceId, int width, int height) {
        Bitmap bitmap = null;
        bitmap = BitmapFactory.decodeResource(resources, resourceId);
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
        
        return bitmap;
    }

    @Override
    public void clear(int color) {
        //canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8, (color & 0xff));
    	canvas.drawColor(color);
    }

    @Override
    public void drawPixel(int x, int y, int color) {
        paint.setColor(color);
        canvas.drawPoint(x, y, paint);
    }

    @Override
    public void drawLine(int x, int y, int x2, int y2, int color) {
        paint.setColor(color);
        canvas.drawLine(x, y, x2, y2, paint);
    }

    @Override
    public void drawRect(int x, int y, int width, int height, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
    }
    
    @Override
    public void drawRect(Rect rect, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawRect(rect, paint);
    }
    
    public void drawCircle(int x, int y, int radius, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawCircle(x, y, radius, paint);
    }
    
    public void drawPath(Path path, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
		canvas.drawPath(path, paint);
    }
    
    @Override
    public void drawOval(int x, int y, int width, int height, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawOval(new RectF(x, y, x + width - 1, y + height - 1), paint);
    }
    
    @Override
    public void drawText(String text, int x, int y, int color, int size, Typeface font) {
    	
    	paint.setColor(color);
    	paint.setTypeface(font);
    	paint.setTextSize(size);
    	
    	canvas.drawText(text, x, y, paint);
    }
    
    @Override
    public Rect getTextBound(String text, int start, int end, Typeface font, int size) {
    	
    	Rect rect = new Rect();
    	paint.setTypeface(font);
    	paint.setTextSize(size);
    	paint.getTextBounds(text, start, end, rect);
    	
    	return rect;
    }

    @Override
    public void drawBitmap(Bitmap bitmap, int x, int y, int srcX, int srcY, int srcWidth, int srcHeight) {
        srcRect.left = srcX;
        srcRect.top = srcY;
        srcRect.right = srcX + srcWidth - 1;
        srcRect.bottom = srcY + srcHeight - 1;

        dstRect.left = x;
        dstRect.top = y;
        dstRect.right = x + srcWidth - 1;
        dstRect.bottom = y + srcHeight - 1;

        canvas.drawBitmap(bitmap, srcRect, dstRect, null);
    }
    @Override
    public void drawBitmap(Bitmap bitmap, Rect r1,Rect r2) {
    	//this.srcRect = r1;
    	//this.dstRect = r2;

        canvas.drawBitmap(bitmap, r1, r2, null);
    }
    @Override
    public void drawBitmap(Bitmap bitmap, int x, int y) {
        canvas.drawBitmap(bitmap, x, y, null);
    }

    @Override
    public int getWidth() {
        return frameBuffer.getWidth();
    }

    @Override
    public int getHeight() {
        return frameBuffer.getHeight();
    }
    
    public void setFrameBuffer(Bitmap frameBuffer) {
    	this.frameBuffer = frameBuffer;
        this.canvas = new Canvas(frameBuffer);
    }
}
